#ifndef SETTING_H
#define SETTING_H

#include <QApplication>
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QTextStream>
#include "mainwindow.h"

void write_file(QString path, bool create_file);

class software_set
{
public:
    bool open_dir_after_create_file;
    bool backup_table_before_change_table;
    QString backup_dir;
    QString StyleColor;

    software_set(bool is_default = false)
    {
        QString path = QDir::currentPath() + "/soft_setting.json";

        if(is_default)
        {
            this -> open_dir_after_create_file = true;
            this -> backup_table_before_change_table = true;
            this -> backup_dir =  QDir::currentPath() + "/backup tables";
            this -> StyleColor = "#1E90FF";
        }
        else
        {
            if(!QFile::exists(path))
            {
                write_file(path, true);
            }

            QFile file(path);
            file.open(QIODevice::ReadOnly);
            QString temp;

            QTextStream stream(&file);
            temp = stream.readAll();

            file.close();

            QJsonDocument json_converter = QJsonDocument::fromJson(temp.toUtf8());
            QJsonObject json_root = json_converter.object();

            this -> open_dir_after_create_file = json_root.value("open_dir_after_create_file").toBool();
            this -> backup_table_before_change_table = json_root.value("backup_table_before_change_table").toBool();
            this -> backup_dir = json_root.value("backup_dir").toString();
            this -> StyleColor = json_root.value("StyleColor").toString();

            QDir dir(this -> backup_dir);
            if(dir.exists() == false)
            {
                dir.mkdir(this -> backup_dir);
            }
        }
    }

    void save_set()
    {
        QString path = QDir::currentPath() + "/soft_setting.json";
        QFile set_file(path);
        if(set_file.exists())
        {
            set_file.remove();
        }

        write_file(path, false);
    }

    static bool DeepColor(QString Color)
    {
        QStringList temp = Color.split(',');
        float r = temp[0].split('(')[1].toFloat();
        float g = temp[1].toFloat();
        float b = temp[2].split(')')[0].toFloat();
        if(r * 0.299f + g * 0.587f + b * 0.114f < 150)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    static void SetWindowStyle(MainWindow* Windows, QString Color)
    {
        QFile StyleFile(":/NormalStyle.qss");
        if(StyleFile.open(QIODevice::ReadOnly))
        {
            QString style = StyleFile.readAll();
            style = style.replace("themecolor", Color);

            /*if(DeepColor(Color))
            {
                style = style.replace("fontcolor", "white");
            }
            else
            {
                style = style.replace("fontcolor", "black");
            }*/

            Windows -> setStyleSheet(style);
        }

        StyleFile.close();
    }
};

software_set* default_setting = new software_set(true);

software_set* setting = nullptr;

void write_file(QString path, bool create_file = false)
{
    software_set* destinate_soft = nullptr;
    if(create_file)
    {
        destinate_soft = default_setting;
    }
    else
    {
        destinate_soft = setting;
    }

    QFile file(path);
    if(file.open(QIODevice::ReadWrite))
    {
        QJsonObject json_obj;
        json_obj.insert("open_dir_after_create_file", destinate_soft -> open_dir_after_create_file);
        json_obj.insert("backup_table_before_change_table", destinate_soft -> backup_table_before_change_table);
        json_obj.insert("backup_dir", destinate_soft -> backup_dir);
        json_obj.insert("StyleColor", destinate_soft -> StyleColor);

        QJsonDocument json_doc;
        json_doc.setObject(json_obj);

        QTextStream stream(&file);
        stream << json_doc.toJson();
    }
    file.close();
}

#endif // SETTING_H
